<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java中sleep()与wait()的区别</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
        }
        
        .highlight-text {
            background: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #667eea, transparent);
            margin: 3rem 0;
        }
        
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                Java中sleep()与wait()的区别
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                深入理解多线程编程中的核心概念
            </p>
            <div class="flex justify-center space-x-8 text-lg">
                <div class="flex items-center">
                    <i class="fas fa-clock mr-2"></i>
                    <span>线程控制</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-sync-alt mr-2"></i>
                    <span>并发协调</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-lock mr-2"></i>
                    <span>锁机制</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>多线程编程中，线程的控制和协调是一个至关重要的课题。合理地管理线程的执行与等待，不仅可以提高程序的性能，还能有效避免潜在的并发问题。
            </p>
            <p class="text-lg leading-relaxed text-gray-700 mt-4">
                想象一下你在开发一个复杂的应用，比如一个在线购物系统。在这个系统中，可能会有多个用户同时访问，进行下单、支付等操作。在这样的环境中，如何有效地管理线程的状态，确保数据的一致性和完整性，就显得尤为重要。
            </p>
        </div>

        <!-- Visual Comparison -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-chart-line feature-icon mr-3"></i>
                核心概念对比
            </h2>
            <div class="mermaid">
                graph TD
                    A[线程控制方法] --> B[sleep方法]
                    A --> C[wait方法]
                    B --> D[Thread类静态方法]
                    B --> E[不释放锁]
                    B --> F[自动恢复]
                    C --> G[Object类方法]
                    C --> H[释放锁]
                    C --> I[需要唤醒]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#84fab0,stroke:#fff,stroke-width:2px
                    style C fill:#8fd3f4,stroke:#fff,stroke-width:2px
            </div>
        </div>

        <!-- Definition Section -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <div class="content-card rounded-xl shadow-xl p-8">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-bed feature-icon mr-2"></i>
                    sleep() 方法
                </h3>
                <ul class="space-y-3 text-gray-700">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span><strong>定义：</strong>Thread类的静态方法</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span><strong>作用：</strong>暂停当前线程执行指定时间</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span><strong>锁行为：</strong>保持已获得的锁</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                        <span><strong>恢复：</strong>时间到达后自动恢复</span>
                    </li>
                </ul>
            </div>
            
            <div class="content-card rounded-xl shadow-xl p-8">
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-pause-circle feature-icon mr-2"></i>
                    wait() 方法
                </h3>
                <ul class="space-y-3 text-gray-700">
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-blue-500 mt-1 mr-3"></i>
                        <span><strong>定义：</strong>Object类的方法</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-blue-500 mt-1 mr-3"></i>
                        <span><strong>作用：</strong>线程等待直到被唤醒</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-blue-500 mt-1 mr-3"></i>
                        <span><strong>锁行为：</strong>释放对象锁</span>
                    </li>
                    <li class="flex items-start">
                        <i class="fas fa-check-circle text-blue-500 mt-1 mr-3"></i>
                        <span><strong>恢复：</strong>需要notify()或notifyAll()唤醒</span>
                    </li>
                </ul>
            </div>
        </div>

        <!-- Code Examples -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-code feature-icon mr-3"></i>
                代码示例
            </h2>
            
            <div class="mb-8">
                <h3 class="text-xl font-semibold mb-4 text-gray-700">sleep() 示例</h3>
                <pre class="code-block p-4"><code>public class SleepDemo {
    public static void main(String[] args) {
        try {
            System.out.println("线程将休眠2秒.");
            Thread.sleep(2000); // 暂停当前线程2秒
            System.out.println("线程被唤醒.");
        } catch (InterruptedException e) {
            System.out.println("线程中断了。");
        }
    }
}</code></pre>
            </div>
            
            <div>
                <h3 class="text-xl font-semibold mb-4 text-gray-700">wait() 示例</h3>
                <pre class="code-block p-4"><code>public class WaitDemo {
    private final Object lock = new Object();

    public void waitingMethod() {
        synchronized (lock) {
            try {
                System.out.println("线程正在等待。");
                lock.wait(); // 释放锁并等待
                System.out.println("线程处于唤醒状态。");
            } catch (InterruptedException e) {
                System.out.println("线程中断了。");
            }
        }
    }
}</code></pre>
            </div>
        </div>

        <!-- Key Differences -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-exchange-alt feature-icon mr-3"></i>
                关键区别总结
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-gradient-to-r from-purple-50 to-pink-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-purple-800">调用对象</h3>
                    <p class="text-gray-700">
                        <strong>sleep()：</strong>Thread类的静态方法，任何地方可调用<br>
                        <strong>wait()：</strong>Object类的方法，需在同步块中调用
                    </p>
                </div>
                
                <div class="bg-gradient-to-r from-blue-50 to-cyan-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-blue-800">锁的管理</h3>
                    <p class="text-gray-700">
                        <strong>sleep()：</strong>不释放任何锁<br>
                        <strong>wait()：</strong>释放对象锁，允许其他线程访问
                    </p>
                </div>
                
                <div class="bg-gradient-to-r from-green-50 to-emerald-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-green-800">恢复方式</h3>
                    <p class="text-gray-700">
                        <strong>sleep()：</strong>时间到达后自动恢复<br>
                        <strong>wait()：</strong>需要notify()或notifyAll()唤醒
                    </p>
                </div>
                
                <div class="bg-gradient-to-r from-yellow-50 to-orange-50 p-6 rounded-lg">
                    <h3 class="text-xl font-semibold mb-3 text-yellow-800">使用场景</h3>
                    <p class="text-gray-700">
                        <strong>sleep()：</strong>定时任务、轮询操作<br>
                        <strong>wait()：</strong>线程间协作、生产者-消费者模式
                    </p>
                </div>
            </div>
        </div>

        <!-- Best Practices -->
        <div class="content-card rounded-xl shadow-xl p-8">
            <h2 class="text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-lightbulb feature-icon mr-3"></i>
                最佳实践建议
            </h2>
            
            <div class="space-y-4">
                <div class="flex items-start">
                    <i class="fas fa-star text-yellow-500 mt-1 mr-3"></i>
                    <p class="text-gray-700">
                        <strong>选择合适的方法：</strong>如果只需要简单的延迟，使用sleep()；如果需要线程间协调，使用wait()。
                    </p>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-star text-yellow-500 mt-1 mr-3"></i>
                    <p class="text-gray-700">
                        <strong>异常处理：</strong>始终妥善处理InterruptedException，避免忽略线程中断信